import '../util/_base_export.dart';
import '../util/_view_export.dart';

import 'package:shared_preferences/shared_preferences.dart';

class AppLangBloc extends ChangeNotifier {
  Locale _appLocale = Locale('en');
  Locale get appLocale => _appLocale;

  static String language = 'en';

  fetchLocale() async {
    var prefs = await SharedPreferences.getInstance();
    String? lv_locale = prefs.getString(AppLocalkey.langCode);
    if (lv_locale == null) lv_locale = 'zh_CN';
    if (lv_locale == 'en')
      language = 'en';
    else
      language = 'zh_CN';
    _appLocale = Locale(lv_locale);

    notifyListeners();
  }

  void changeLanguage(Locale type) async {
    if (_appLocale == type) {
      return;
    }
    if (type == Locale("zh")) {
      _appLocale = Locale("zh");
      await AppSession.prefs.setString(AppLocalkey.langCode, 'zh');
      await AppSession.prefs.setString(AppLocalkey.langCountry, '');
      language = 'zh_CN';
    } else {
      _appLocale = Locale("en");
      await AppSession.prefs.setString(AppLocalkey.langCode, 'en');
      await AppSession.prefs.setString(AppLocalkey.langCountry, '');
      language = 'en';
    }
    notifyListeners();
  }
}
